package com.pi.pro.xcmg.am.was.fac.handler;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.pi.common.utils.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 *
 */
public class WorkDataHolder {

    private Cache<String, Long> palletAsnCache= CacheBuilder.newBuilder().expireAfterWrite(40, TimeUnit.MINUTES).build();


    /**
     * 码盘数据/托盘-ASN
     */
    public String savePalletContainnumberWithAsn(String containerNumber, Long documentId){
        String m="";
        if (StringUtils.isNullOrBlank(containerNumber)){
            m="container number is blabk";
            return m;
        }
        if (documentId==null){
            m="document id is null";
            return m;
        }
        this.palletAsnCache.put(containerNumber.trim(), documentId);
        return m;
    }

    public Long takeAsnByPalletContainnumber(String containerNumber){
        Long documentId=null;
        if (StringUtils.isNullOrBlank(containerNumber)){
            return documentId;
        }
        documentId=this.palletAsnCache.getIfPresent(containerNumber.trim());
        return documentId;
    }

    public String cleanPalletContainnumberOnAsn(String containerNumber){
        String m="";
        Long documentId=this.takeAsnByPalletContainnumber(containerNumber);
        if (documentId==null){
            return m;
        }
        try{
            this.palletAsnCache.invalidate(containerNumber.trim());
        }catch (Exception e){
            e.printStackTrace();
        }
        return m;
    }
}
